﻿using System;
using System.Collections.Specialized;
using Engine;
using Microsoft.Xna.Framework;
using ThinkDotNetEngine.Bots.Ammos;

namespace ThinkDotNetEngineDrawer.Bots.Ammos
{
    public abstract class AmmoDrawerBase
    {
        private readonly Type SupportableAmmoType;

        protected AmmoDrawerBase(Game game,Type supportableAmmoType)
        {
            SupportableAmmoType = supportableAmmoType;
            Game = game;
        }

        #region Public Methods

        public virtual void Initialize()
        {
            AmmoHandler = (IAmmoHandler) Game.Services.GetService(typeof (IAmmoHandler));
            World = (World) Game.Services.GetService(typeof (World));
            AmmoHandler.AmmosCollectionChanged +=OnAmmosCollectionChanged;
        }
        public abstract void DrawEffects(GameTime gameTime, AmmoBase ammo);

        #endregion

        #region Events

        private void OnAmmosCollectionChanged(object sender,
                                                        NotifyCollectionChangedEventArgs
                                                            e)
        {
            if(e.Action==NotifyCollectionChangedAction.Add)
            {
                foreach (AmmoBase ammoBase in e.NewItems)
                {
                    if(ammoBase.GetType()==SupportableAmmoType)
                       OnAmmoAdded(ammoBase);
                }
            }
            else if (e.Action == NotifyCollectionChangedAction.Remove)
            {
                foreach (AmmoBase ammoBase in e.OldItems)
                {
                    if (ammoBase.GetType() == SupportableAmmoType)
                        OnAmmoRemoved(ammoBase);
                }
            }
        }
        protected virtual void OnAmmoAdded(AmmoBase ammoBase){}
        protected virtual void OnAmmoRemoved(AmmoBase ammoBase) { }

        #endregion

        #region Properties

        public IAmmoHandler AmmoHandler { get; private set; }
        public World World { get; private set; }
        public Game Game { get; private set; }

        #endregion
    }
}
